package com.controller;

/**
 * Created by wlc on 2018/4/14.
 */

import com.entity.User;
import com.entity.UserMetadata;
import com.util.CookieUtil;
import com.util.LocalInformation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

import static com.service.UserOperator.changeUserInformation;
import static com.service.UserOperator.getUserInformation;

/**
 * 映射用户管理部分
 */
@Controller
@RequestMapping(value = "/userManager",produces = "text/html;charset=UTF-8")
public class UserManagerController {
    //映射到用户中心主页
    @RequestMapping(value = "/UserCenter",produces = "text/html;charset=UTF-8")
    public ModelAndView UserCenter(HttpServletRequest request) throws Exception{
        ModelAndView modelAndView = new ModelAndView();

        String user_cookie_value = CookieUtil.getUserCookie(request).getValue();
        if (user_cookie_value != null && UserMetadata.OnlineUserId.containsKey(user_cookie_value)){
            UserMetadata.OnlineUserOperatorTime.replace(user_cookie_value, LocalInformation.getTimeID());
            String projectPath = request.getSession().getServletContext().getRealPath("/");

            User user = getUserInformation(user_cookie_value.split("wlcniubi")[0],projectPath);

            modelAndView.addObject("user",user);
            modelAndView.setViewName("User-Center");
        }else {
            modelAndView.setViewName("login");
        }
        return modelAndView;
    }

    //映射到用户信息修改
    @RequestMapping(value = "/saveUserInfo",produces = "text/html;charset=UTF-8")
    public void saveUserInfo(HttpServletRequest request, HttpServletResponse response,User user) throws Exception{
        String user_cookie_value = CookieUtil.getUserCookie(request).getValue();
        String projectPath = request.getSession().getServletContext().getRealPath("/");

        user.setId(user_cookie_value.split("wlcniubi")[0]);
        user.setUsername(user_cookie_value.split("wlcniubi")[1]);
        user.setPermissions(user_cookie_value.split("wlcniubi")[2]);
        user.setLogin_time(user_cookie_value.split("wlcniubi")[3]);
        changeUserInformation(user,projectPath);

        PrintWriter out= null;
        out = response.getWriter();
        out.print("true");
        out.flush();
        out.close();
    }

    //映射到判断密码正确
    @RequestMapping(value = "/verifiPass",produces = "text/html;charset=UTF-8")
    public void verifiPass(HttpServletRequest request, HttpServletResponse response,User user) throws Exception{
        String user_cookie_value = CookieUtil.getUserCookie(request).getValue();
        String projectPath = request.getSession().getServletContext().getRealPath("/");
        PrintWriter out= null;
        out = response.getWriter();

        User oldUser = getUserInformation(user_cookie_value.split("wlcniubi")[0],projectPath);
        if (oldUser.getPassword().equals(user.getPassword())){
            out.print("true");
        }else {
            out.print("false");
        }
        out.flush();
        out.close();
    }

    //映射到密码修改
    @RequestMapping(value = "/changePass",produces = "text/html;charset=UTF-8")
    public void changePass(HttpServletRequest request, HttpServletResponse response,User user) throws Exception{
        String user_cookie_value = CookieUtil.getUserCookie(request).getValue();
        String projectPath = request.getSession().getServletContext().getRealPath("/");
        PrintWriter out= null;
        out = response.getWriter();

        user.setId(user_cookie_value.split("wlcniubi")[0]);
        user.setUsername(user_cookie_value.split("wlcniubi")[1]);
        user.setPermissions(user_cookie_value.split("wlcniubi")[2]);
        user.setLogin_time(user_cookie_value.split("wlcniubi")[3]);
        changeUserInformation(user,projectPath);
        out.print("true");
        out.flush();
        out.close();
    }
}
